{%TEMPLATE->struct/head%}
{%TEMPLATE->struct/menu%}
<h2>Health Checks</h2>
{%TEMPLATE->struct/messages%}
<div class="alert alert-info">
  Health checks can be used to check if all agents are working properly. Note that a health check won't be finished until all registered agents report back their results.
</div>
<div class="card">
  <div class="table-responsive">
    <table class="table table-bordered">
		  <tr>
			  <th>Check ID</th>
			  <th>Created</th>
			  <th>Type</th>
			  <th>Status</th>
			  <th>&nbsp;</th>
		  </tr>
		  {{FOREACH check;[[checks]]}}
			  <tr>
				  <td>[[check.getId()]]</td>
				  <td>[[date([[config.getVal([[$DConfig::TIME_FORMAT]])]], [[check.getTime()]])]]</td>
				  <td>{{IF [[check.getCheckType()]] == [[$DHealthCheckType::BRUTE_FORCE]]}}Brute-Force{{ENDIF}} ([[Util::getHashtypeById([[check.getHashtypeId()]])]])</td>
				  <td>
            {{IF [[check.getStatus()]] == [[$DHealthCheckStatus::PENDING]]}}
              Running
            {{ENDIF}}
            {{IF [[check.getStatus()]] == [[$DHealthCheckStatus::COMPLETED]]}}
              Completed
            {{ENDIF}}
            {{IF [[check.getStatus()]] == [[$DHealthCheckStatus::ABORTED]]}}
              Aborted
            {{ENDIF}}
          </td>
          <td>
            <form action="health.php" method="POST" class="float-right" onSubmit="if (!confirm('Really delete/abort health check [[check.getId()]]?')) return false;">
              <input type='hidden' name='action' value='[[$DHealthCheckAction::DELETE_HEALTH_CHECK]]'>
              <input type="hidden" name="healthCheckId" value="[[check.getId()]]">
              <input type="hidden" name="csrf" value="[[csrf]]">
              <button type="submit" class='btn btn-danger' data-toggle="tooltip" data-placement="top" title="Delete"><i class="fas fa-trash" aria-hidden="true"></i></button>
            </form>
            <a href="health.php?id=[[check.getId()]]" class="float-right mx-1 btn btn-primary">View</a>
          </td>
			  </tr>
		  {{ENDFOREACH}}
      {{IF [[sizeof([[checks]])]] == 0}}
        <tr><td>---</td><td>---</td><td>---</td><td>---</td><td>---</td></tr>
      {{ENDIF}}
      <tr>
        <td colspan="5">
          <form class="form-inline" method="post" action="">
            <input type="hidden" name="action" value="[[$DHealthCheckAction::CREATE]]">
            <input type="hidden" name="csrf" value="[[csrf]]">
            <select class="form-control mx-1" name="type" title="Type">
              <option value="0">Brute-Force</option>
            </select>
            <select class="form-control mx-1" name="hashtypeId" title="Hash Type">
              <option value="0">MD5</option>
              <option value="3200">BCRYPT</option>
            </select>
            <select name="crackerBinaryTypeId" id="crackerBinaryType" class="form-control mx-1" title="Cracker Binary">
              {{FOREACH binary;[[binaries]]}}
                <option value="[[binary.getId()]]">[[binary.getTypeName()]]</option>
              {{ENDFOREACH}}
            </select>
            <select name="crackerBinaryVersionId" id="crackerBinaryVersion" class="form-control" title="Binary Version">
              <option value="0">&nbsp;</option>
            </select>
            <script type="text/javascript">
              var versions = [];
              {{FOREACH version;[[versions]]}}
                if ([[version.getCrackerBinaryTypeId()]] in versions) {
                    versions[ [[version.getCrackerBinaryTypeId()]] ].push(["[[version.getVersion()]]", [[version.getId()]] ]);
                }
                else{
                  versions [ [[version.getCrackerBinaryTypeId()]] ] = [ ["[[version.getVersion()]]", [[version.getId()]] ] ];
                }
              {{ENDFOREACH}}

              configureDropDownLists(document.getElementById('crackerBinaryType'), document.getElementById('crackerBinaryVersion'));
              $("#crackerBinaryType").change(function() {
                  configureDropDownLists(document.getElementById('crackerBinaryType'), document.getElementById('crackerBinaryVersion'));
              });

              function configureDropDownLists(ddl1, ddl2) {
                ddl2.options.length = 0;
                if (ddl1.value.length === 0) {
                    return;
                }
                for (i = 0; i < versions[ddl1.value].length; i++){
                    createOption(ddl2, versions[ddl1.value][i][0], versions[ddl1.value][i][1]);
                }
              }

              function createOption(ddl, text, value) {
                var opt = document.createElement('option');
                opt.value = value;
                opt.text = text;
                ddl.options.add(opt);
              }
            </script>
            <button type="submit" class="btn btn-success mx-1">Create</button>
          </form>
        </td>
      </tr>
	  </table>
  </div>
</div>
{%TEMPLATE->struct/foot%}
